#include "usermodel.h"
UserModel* UserModel ::instance = nullptr;

UserModel *UserModel::getInstance()
{
    if(UserModel::instance == nullptr)
    {
        UserModel::instance = new UserModel;
    }
    return UserModel::instance;
}

int UserModel::selectUserByNameAndPwd(QString name, QString pwd)
{
    //1.拼接sql
    //2.数据库单例执行
    //3.row返回结果

    QString sql = QString("select * from tbl_user where user_name= '%1' and user_password = '%2';").arg(name).arg(pwd);
    qDebug() << "selectUserByNameAndPwd-21:" << sql;
    char **qres=nullptr;
    int row=0,col=0;
    //4.调用查询函数  row==1  登陆成功  else登陆失败      row==0可以注册 else 注册失败
    int res = SingletonBD::getInstance()->doSelect(sql, qres, row, col);
    if(res==0)
    {
        if(row>0)       //有数据   登陆成功    更新用户状态
        {
            return 0;
        }
    }
    return -1;  //返回默认值
}

int UserModel::updateUserByName(QString name)
{
    QString sql = QString("UPDATE tbl_user set user_status = 1 WHERE user_name = '%1';").arg(name);
    qDebug() << "username = " << name;
    int res = SingletonBD::getInstance()->doUpdateInsertDel(sql);
    return res;
}

int UserModel::updateUserAll()
{
    QString sql = QString("update tbl_user set user_status = 0;");
    int res = SingletonBD::getInstance()->doUpdateInsertDel(sql);
    return res;
}

int UserModel::selectUserId(char **&qres, int &row, int &col)
{
    QString sql = QString("select user_id from tbl_user where user_status = 1;");
    //4.调用查询函数  row==1  登陆成功  else登陆失败      row==0可以注册 else 注册失败
    int res = SingletonBD::getInstance()->doSelect(sql, qres, row, col);
    if(res==0)
    {
        if(row>0)       //有数据   登陆成功    更新用户状态
        {
            return 0;
        }
    }
    return -1;  //返回默认值
}


UserModel::UserModel()
{

}
